package com.book.controller;

import com.book.entity.Student;
import com.book.service.StudentService;
import com.book.service.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by dell on 2017/6/14.
 */
@Controller
public class StudentController {

    @Autowired
    StudentService studentService;


    //访问这个controller都需要加路径/user
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "/login";
    }

    //构造函数
//    public StudentController() {
//        //将值添加进去
//        student.put("zl", new Student("zl", "123", "qq.com", "赵磊"));
//        student.put("dzq", new Student("dzq", "123", "fox.com", "邓紫棋"));
//        student.put("hjc", new Student("hjc", "123", "like.com", "洪继成"));
//        student.put("pm", new Student("pm", "123", "136.com", "泡面"));
//        student.put("wyt", new Student("wyt", "123", "wyt.com", "五月天"));
//
//
//    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String studentList(Student student, Model model) {
        Student student1 = studentService.login(student.getStudentName(), student.getStudentPassword());
        if (student1.getStudentId() != null) {
            model.addAttribute("loginUser", student1);
        }
        return "redirect:/list";
    }


    //获取所有用户的信息
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        //      查  连接数据库，将数据库中的数据显示在页面上
        List<Student> StudentList = studentService.select();
        model.addAttribute("listStudent", StudentList);
        return "user/list";
    }

    //借助数组进行分页
    @ResponseBody
    @RequestMapping("/list/{currPage}/{pageSize}")
    public List<Student> getStudentByArray(@PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize) {
        List<Student> student = studentService.queryStudentsByArray(currPage, pageSize);
        return student;
    }

    //添加用户的信息
    //连接到add.jsp页面时，是get请求，访问这段代码
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model, Student student) {


        return "user/add";
    }
    //将student放到model中
    /*第一种开启modelDriven的方式,放到model中
    public String add(Model model){
            model.addAttribute(new Student());
               return "user/add";
            */


//    第二种方法开启modelDriven的方式，将student放到model中
//    public String add(@ModelAttribute("student")Student studentr) {
//        服务器端跳转
//        return "user/add";
//    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addp(Model model, Student student) {
        int n = studentService.insert(student);
        if (n > 0) {
            return "redirect:/list";
        } else {
            return "添加失败";
        }
    }

    @RequestMapping(value = "{studentId}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable String studentId) {

        System.out.println("=======================收到的id"+studentId);
        int n = studentService.deleteByPrimaryKey(Integer.valueOf(studentId));
        if (n > 0) {
            return "redirect:/list";
        } else {
            return "删除失败";
        }

    }

    //得到上传文件将要放置的文件夹路径
//    String realpath=req.getSession().getServletContext().getRealPath("/resources/upload");
//    System.out.println(realpath);
//    for (MultipartFile attach:attachs) {
//        if (attach.isEmpty()) continue;
//        File f = new File(realpath + "/" + attach.getOriginalFilename());
//        FileUtils.copyInputStreamToFile(attach.getInputStream(), f);


//    System.out.println(attach.getName() + "," + attach.getOriginalFilename() + "," + attach.getContentType());
//}
//workers.put(worker.getWorkername(), worker);
//        客户端跳转
//        return "redirect:/worker/workers";
//
//        }

    @RequestMapping(value = "{studentId}/update", method = RequestMethod.GET)
    public String update(@PathVariable String studentId,Model model,Student student) {
        System.out.println("=======================收到的id"+studentId);
        student.setStudentId(Integer.valueOf(studentId));
        model.addAttribute("updateStudent",studentService.selectByPrimaryKey(student));

            return "user/update";


    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updatep(Student student, Model model) {
        System.out.println("收到的值"+student.getStudentName());

     int  n =  studentService.updateByPrimaryKey(student);

        if(n>0){
            model.addAttribute("student",studentService.selectByPrimaryKey(student));
            return "user/userinfo";
        }else {
            return "修改失败";
        }

    }
}
